//
//  MovingTabBarVC.m
//  TT
//
//  Created by Lenny on 16/4/14.
//  Copyright © 2016年 Lenny. All rights reserved.
//

#import "MovingTabBarVC.h"
#import "ViewController.h"
//VCs
#import "PublicVC.h"
#import "EnduranceVC.h"
#import "CompanyVC.h"

//views
#import "MessageV.h"

@interface MovingTabBarVC ()
{
    NSMutableArray *_allBtns;
}

@property(nonatomic,strong)MessageV *messageV;
@end

@implementation MovingTabBarVC

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor greenColor];
    
    [self setData];
    [self setTabBar];
    [self.view addSubview:self.messageV];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - event methods
- (void)btnAction:(UIButton *)sender {
    
    if (sender == self.selectBtn) {
        
        return;
    }
    self.selectBtn.selected = NO;
    sender.selected = YES;
    [self refreshStateWithBtn:self.selectBtn];
    [self refreshStateWithBtn:sender];
    self.selectBtn = sender;
    
    NSInteger selectIndex = sender.tag;
    
    if (selectIndex == 2) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    if (selectIndex == 1) {
        
        return;
    }

    if (selectIndex != 2) {
        //切换视图
        self.selectedIndex = selectIndex;
    }
}

- (void)longPressGesture:(UILongPressGestureRecognizer *)gesture {

    UIButton *btn = (UIButton *)gesture.view;
    if (gesture.state == UIGestureRecognizerStateBegan) {
        
        WLLog(@"开始录音");
        [self btnAction:btn];
        self.messageV.hidden = NO;
        
    }else if (gesture.state == UIGestureRecognizerStateEnded) {
        
        WLLog(@"结束录音");
        self.messageV.hidden = YES;
        [self btnAction:_allBtns[0]];
    }
}

#pragma mark - private methods

- (void)setData {
    
    _allBtns = [NSMutableArray array];
}

- (void)setTabBar {
    
    
    PublicVC *publicVC = [[PublicVC alloc] init];
    UINavigationController *publicNavi = [[UINavigationController alloc] initWithRootViewController:publicVC];
    
    EnduranceVC *enduranceVC = [[EnduranceVC alloc] init];
    UINavigationController *enduranceNavi = [[UINavigationController alloc] initWithRootViewController:enduranceVC];
    enduranceNavi.view.backgroundColor = [UIColor blueColor];
    
    ViewController *vc2 = [[ViewController alloc] init];
    ViewController *vc4 = [[ViewController alloc] init];
    
    CompanyVC *companyVC = [[CompanyVC alloc] init];
    UINavigationController *companyNavi = [[UINavigationController alloc] initWithRootViewController:companyVC];
    
    self.viewControllers = @[publicNavi,vc2,vc4,enduranceNavi,companyNavi];
    
    NSArray *titles = @[@"公放",@"留言",@"熄火",@"续航",@"做伴"];
    for (UITabBarItem *item in self.tabBar.items) {
        
        NSInteger index = [self.tabBar.items indexOfObject:item];
        item.title  = titles[index];
    }
    self.tabBar.hidden = YES;
    
    [self.view addSubview:self.tabBarView];
}

- (void)refreshStateWithBtn:(UIButton *)sender {
    
    UIImageView *imageView = sender.subviews[0];
    UILabel *nameLabel = sender.subviews[1];
    if (sender.selected) {
        
        nameLabel.textColor = WL_M_Bar_H_Color;
        imageView.highlighted = YES;
    }else {
        nameLabel.textColor = [UIColor whiteColor];
        imageView.highlighted = NO;
    }
}





#pragma mark - setters and getters

- (UIView *)tabBarView {
    
    if (_tabBarView == nil) {
        
        _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, k_screen_h - k_M_Tabbar_h, k_screen_w, k_M_Tabbar_h)];
        _tabBarView.backgroundColor = WL_M_Bar_Back_Color;
        
        NSInteger num = self.tabBar.items.count;
        CGFloat wide = k_screen_w / num;
        for (int i = 0; i < num; i ++) {
            
            UITabBarItem *item = self.tabBar.items[i];
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(wide * i, 0, wide, k_M_Tabbar_h)];
            [_tabBarView addSubview:btn];
            [_allBtns addObject:btn];
            btn.tag = i;
            if (i != 1) {
            
                [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            }else {
                
                UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
                longPress.minimumPressDuration = 0.1;
                [btn addGestureRecognizer:longPress];
            }
        
           
            if (i == 0) {
                btn.selected = YES;
                self.selectBtn = btn;
            }
            
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_tabbar_m_%d_normal",i]];
            UIImage *selectImage = [UIImage imageNamed:[NSString stringWithFormat:@"icon_tabbar_m_%d_select",i]];
            CGSize imageSize = image.size;
            if (i == 2) {
                
                btn.frame = CGRectMake(btn.frame.origin.x, -5, wide, k_M_Tabbar_h + 5);
                btn.backgroundColor = WL_M_Bar_H_Color;
            }
            
            UIImageView *btnImageView = [[UIImageView alloc] init];
            btnImageView.image = image;
            btnImageView.highlightedImage = selectImage;
            [btn addSubview:btnImageView];
            [btnImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.size.mas_equalTo(imageSize);
                make.centerY.equalTo(btn).offset(-10);
                make.centerX.equalTo(btn);
            }];
            
            UILabel *nameLabel = [[UILabel alloc] init];
            [btn addSubview:nameLabel];
            nameLabel.text = item.title;
            nameLabel.textColor = [UIColor whiteColor];
            nameLabel.font = [UIFont systemFontOfSize:13];
            [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.top.equalTo(btnImageView.mas_bottom).offset(3);
                make.centerX.equalTo(btn);
            }];
            
            [self refreshStateWithBtn:btn];
        }
    }
    return _tabBarView;
}

- (MessageV *)messageV {
    
    if (_messageV == nil) {
        
        _messageV = [[MessageV alloc] initWithFrame:self.view.bounds];
        _messageV.hidden = YES;
    }
    return _messageV;
}

@end
